"""
Case Type   : DSS元数据修复工具
Case Name   : dsstbox参数异常值验证
Create At   : 2025/04
Owner       : haomeng
Description :
    1、查看当前卷名
    2、修改software_version类型的值，指定type未非特定值
    3、修改元数据部分参数缺失or冗余
    4、修改元数据，指定type在非入口卷
Expect      :
    1、查看成功
    2、修改失败
    3、修改失败
    4、修改失败
History     :
"""

import os
import time
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class DSSrepairtools(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.user_node = Node('PrimaryDbUser')

        self.vol_path = os.path.join(macro.DSS_INSTANCE_PATH, 'cfg/dss_vg_conf.ini')
        self.core_ctrl_meta = ['version', 'volume_count', 'au_size']
        self.rf_block_meta = 'ft_root-ft_root_free_list-last'
        self.core_ctrl_meta_value = [3707, 2, 8192]

    def test_dsstbox(self):
        text = '-----step1:查看当前卷名;  expect:查看成功-----'
        self.log.info(text)

        res = self.user_node.sh(f"cat {self.vol_path} | awk 'NR==1'").result()
        self.log.info(res)
        vol_name = res.split(':')[0]
        vol_value = res.split(':')[1]
        self.log.info(f'vol_name的值是：{vol_name}, vol_value的值是：{vol_value}')
        self.assertNotEqual('', vol_name, '查看当前卷名失败')

        text = '-----step2:修改software_version类型的值，指定type未非特定值  expect:修改失败-----'
        self.log.info(text)

        cmd = f"source {macro.DB_ENV_PATH};dsstbox ssrepair -v {vol_value} -t software_version -k 'version=1000'"
        self.log.info(cmd)
        execute_cmd = f'''source {macro.DB_ENV_PATH};yes|{cmd}'''
        self.log.info(execute_cmd)
        res = self.user_node.sh(execute_cmd).result()
        self.log.info(res)
        self.assertIn('Invalid intput key_value', res, '修改元数据成功')

        text = '-----step3:修改元数据部分参数缺失or冗余;  expect:修改失败-----'
        self.log.info(text)

        key_value = ''
        for param in self.core_ctrl_meta:
            key_value += f'{param}={self.core_ctrl_meta_value[self.core_ctrl_meta.index(param)]};'
            self.log.info(key_value)

        cmd1 = f"source {macro.DB_ENV_PATH};dsstbox ssrepair -v {vol_value} -t core_ctrl root_ft_block -k '{key_value}'"
        self.log.info(cmd1)
        execute_cmd1 = f'''source {macro.DB_ENV_PATH};yes|{cmd1}'''
        self.log.info(execute_cmd1)
        res1 = self.user_node.sh(execute_cmd1).result()
        self.log.info(res1)
        self.assertIn('ssrepair execute failed', res1, '修改元数据成功')

        cmd2 = f"source {macro.DB_ENV_PATH};dsstbox ssrepair -t root_ft_block -k '{self.rf_block_meta}=123'"
        self.log.info(cmd2)
        execute_cmd2 = f'''source {macro.DB_ENV_PATH};yes|{cmd2}'''
        self.log.info(execute_cmd2)
        res2 = self.user_node.sh(execute_cmd2).result()
        self.log.info(res2)
        self.assertIn('ssrepair execute failed', res2, '修改元数据成功')

        text = '-----step3:修改元数据，指定type在非入口卷;  expect:修改失败-----'
        self.log.info(text)

        cmd3 = f"source {macro.DB_ENV_PATH};dsstbox ssrepair -v {vol_value}_no -t root_ft_block -k '{self.rf_block_meta}=1000'"
        self.log.info(cmd3)
        self.user_node.sh(cmd3).result()
        res3 = self.user_node.sh(cmd3).result()
        self.log.info(res3)
        self.assertIn('verify disk version failed', res3, '修改元数据成功')

    def tearDown(self):
        self.log.info('-----No need to clean-----')
        self.log.info(f'----{os.path.basename(__file__)} end----')
